﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.smslib.modem.athandler;
using org.smslib.modem;

namespace SmsUI.SmsCore
{
    public class ATHandler_Wavecom_AddCall : ATHandler_Wavecom
    {

        public ATHandler_Wavecom_AddCall(ModemGateway myGateway)
            : base(myGateway)
        {
        }
        public override string sendCustomATCommand(string atCommand)
        {
            bool re = IsTrueNum(atCommand);
            return re.ToString();
        }
        //public override bool setIndications()
        //{
        //    getModemDriver().write("AT+CNMI=3,1,0,2,0\r");
        //    getModemDriver().getResponse();
        //    return false;
        //}
        //public override string getNetworkRegistration()
        //{

        //    return "AT+CREG=1,1";
        //}
        public override bool setPduProtocol()
        {
            try
            {
                base.setPduProtocol();
            }
            catch
            {
            }
            return true;
        }
        public bool IsTrueNum(string num)
        {
            string readBuffer;
            bool isSucc = false;
            getModemDriver().write(String.Format("ATD {0};\r", num));
            int atCount = 0;
            while (true)
            {
                try
                {
                   
                    atCount++;
                    if (atCount > 30)
                    {
                        isSucc = false;
                        break;
                    }
                    if (SmsHelper.SmsHelper.Instance.checkNumCts.IsCancellationRequested)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(500);
                    getModemDriver().write("AT+CLCC\r");
                    readBuffer = getModemDriver().getResponse();
                    if ((readBuffer.IndexOf("+CLCC: 1,0,3") != -1) || (readBuffer.IndexOf("BUSY") != -1) || (readBuffer.IndexOf("ANSWER") != -1) || (readBuffer.IndexOf("+CLCC: 1,0,0") != -1) || (readBuffer.IndexOf("FFFF") != -1))
                    {
                        //一个正振铃的电话或被挂断的电话，号码有效
                        isSucc = true;
                        break;
                    }
                    else if (readBuffer.IndexOf("+CLCC: 1,0,2") != -1)  //一个正拨打的电话，还未振铃
                    {
                        readBuffer = "";
                    }
                  
                
                    //else if (readBuffer.IndexOf("OK") != -1)     //如果当前没有电话在工作，结束判断，无效号码
                    //{
                    //    readBuffer = "";
                    //    break;
                    //}
                    else if ((readBuffer.IndexOf("NO CARRIER") != -1) || (readBuffer.IndexOf("ERROR") != -1))
                    {
                        //始终没有等到振铃，号码无效，结束判断
                        break;
                    }
                }
                catch
                {
                    isSucc = false;
                    break;
                }
            }
            try
            {
                getModemDriver().write("ATH\r");
                getModemDriver().clearBuffer();
            }
            catch
            {
            }

            //ATH 呼叫挂起

            return isSucc;
        }
        public override void done()
        {
            try
            {
                getModemDriver().write((char)26);
            }
            catch
            {
            }

            //base.done();
        }
        public override void reset()
        {

            //base.reset();
        }

        public override string getSimStatus()
        {
            //getModemDriver().write("AT\r");
            //getModemDriver().getResponse();

            return "OK";
        }
        public override string getNetworkRegistration()
        {

            return "AT+CREG=1,1";
        }

        public override bool setIndications()
        {
            getModemDriver().write("AT+CNMI=2,1,0,2,0\r");
            getModemDriver().getResponse();

            //Console.WriteLine("Indi" + getIndications());

            //try
            //{
            //    base.setIndications();
            //}
            //catch
            //{
            //    Console.WriteLine("Indi Error" + getIndications());
            //}


            return true;
        }
    }
}
